package com.cfc.stage1;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 杨辉三角
 *   https://leetcode.cn/problems/pascals-triangle/
 * @author: yangxuan10
 * @create: 2024-04-26 13:41
 **/
public class Generate {

    @Test
    public void test1() {

        System.out.println(generate3(2));
    }

    public List<List<Integer>> generate3(int numRows) {
        List<List<Integer>> res = new ArrayList<>();
        int[][] nums = new int[numRows + 1][numRows + 1];
        nums[1][1] = 1;

        List<Integer> l1 = new ArrayList<>();
        l1.add(nums[1][1]);
        res.add(l1);

        for (int i = 2; i <= numRows; i++) {
            List<Integer> list = new ArrayList<>();
            for (int j = 1; j <= i; j++) {
                if (j == 1) {
                    nums[i][j] = nums[i - 1][j];
                } else if (j == i) {
                    nums[i][j] = nums[i - 1][i - 1];
                } else {
                    nums[i][j] = nums[i - 1][j - 1] + nums[i - 1][j];
                }
                list.add(nums[i][j]);
            }
            res.add(list);
        }
        return res;
    }

    public List<List<Integer>> generate2(int numRows) {
        List<List<Integer>> allList = new ArrayList<>();
        for (int i = 0; i < numRows; ++i) {
            List<Integer> newList = new ArrayList<>();
            for (int j = 0; j <= i; ++j) {
                if (j == 0 || j == i) {
                    newList.add(1);
                } else {
                    List<Integer> preList = allList.get(i - 1);
                    newList.add(preList.get(j - 1) + preList.get(j));
                }
            }
            allList.add(newList);
        }
        return allList;
    }

    /**
     * 这里给稍加优化了一下呢
     *
     * @param numRows
     * @return
     */
    public List<List<Integer>> generate1(int numRows) {
        List<List<Integer>> allList = new ArrayList<>();
        List<Integer> preList = new ArrayList<>();
        for (int i = 0; i < numRows; ++i) {
            List<Integer> newList = new ArrayList<>();
            for (int j = 0; j <= i; ++j) {
                if (j == 0 || j == i) {
                    newList.add(1);
                } else {
                    newList.add(preList.get(j - 1) + preList.get(j));
                }
            }
            preList = newList;
            allList.add(newList);
        }
        return allList;
    }

    /**
     * numRows = 1 与 numRows =2
     * 是小概率发生事件，但是占用了多数的代码编写行数，是不太友好呢
     *
     * @param numRows
     * @return
     */
    public List<List<Integer>> generate(int numRows) {

        List<List<Integer>> allList = new ArrayList<>();
        List<Integer> currentNumList = new ArrayList<>();
        currentNumList.add(1);
        allList.add(currentNumList);
        if (numRows == 1) {
            return allList;
        }
        currentNumList = new ArrayList<>();
        currentNumList.add(1);
        currentNumList.add(1);
        allList.add(currentNumList);
        if (numRows == 2) {
            return allList;
        }
        List<Integer> preNumList = currentNumList;
        for (int i = 2; i < numRows; i++) {
            List<Integer> newList = new ArrayList<>();
            newList.add(1);
            for (int j = 0; j < preNumList.size() - 1; j++) {
                newList.add(preNumList.get(j) + preNumList.get(j + 1));
            }
            newList.add(1);
            preNumList = newList;
            allList.add(newList);
        }
        return allList;
    }
}